﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MvcApp
{
  public class Startup
  {

    const string DefaultChallengeScheme = "oidc";

    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.Configure<CookiePolicyOptions>(options =>
      {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
      });


      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

      // 使用cookie认证
      //app.UseCookieAuthentication(new CookieAuthenticationOptions
      //{
      //  AuthenticationScheme = "Cookies"
      //});

      services.AddAuthentication(options =>
      {
        options.DefaultScheme = "Cookies";

        options.DefaultChallengeScheme = DefaultChallengeScheme;
      })
      .AddCookie("Cookies")
      .AddOpenIdConnect(DefaultChallengeScheme, options =>
       {

         options.SignInScheme = "Cookies";

         options.Authority = "http://localhost:5500";
         options.RequireHttpsMetadata = false;

         options.ClientId = "mvc";
         options.ClientSecret = "secret";

         options.ResponseType = "code id_token";


         options.GetClaimsFromUserInfoEndpoint = true;

         options.SaveTokens = true;

         options.Scope.Add("api3");

         options.Scope.Add("offline_access");

         options.ClaimActions.MapJsonKey("website", "website");
       });

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
      //  JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

      loggerFactory.AddConsole(Configuration.GetSection("Logging"));
      loggerFactory.AddDebug();

      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseExceptionHandler("/Home/Error");
      }

      app.UseStaticFiles();
      app.UseCookiePolicy();

      app.UseAuthentication();

      app.UseMvc(routes =>
      {
        routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
      });
    }
  }
}
